package persistence.Impl;
import java.sql.*;

/**
 * @projectName: petStore
 * @package: persistence.Impl
 * @className: DEUtil
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/10/30 19:57
 * @version: 1.0
 */


public class DBUtil {
    //112.74.55.242:3306
    private static String url = "jdbc:mysql://112.74.55.242:3306/webdata?useSSL=false&useUnicode=true&characterEncoding=utf8";
    private static String user = "root";
    private static String password = "123456";
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() {
        Connection conn=null;
        try {
            conn=DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void close(Connection conn,Statement stm,ResultSet rs) {
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stm!=null) {
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection connection)throws Exception{
        if(connection!=null){
            connection.close();
        }
    }
    public static void closeStatement(Statement statement)throws Exception{
        if(statement!=null){
            statement.close();
        }
    }
    public static void closePreparedStatement(PreparedStatement preparedStatement)throws Exception{
        if(preparedStatement!=null){
            preparedStatement.close();
        }
    }
    public static void closeResultSet(ResultSet resultSet)throws Exception{
        if(resultSet!=null){
            resultSet.close();
        }
    }
    public static void main(String[] args) {
      Connection connection=DBUtil.getConnection();
        System.out.println(connection);
    }
}

